#include "myshm.hpp"
#include <unistd.h>

int main()
{
    // 1. 创建共享内存
    key_t key = getKey(); // 用户层面标识共享内存的key
    std::cout << "key:" << key << std::endl;
    int shmId = createShm(key);
    std::cout << "shmId:" << shmId << std::endl;

    // 2. 将进程与共享内存连接起来
    char *start = (char *)attachShm(shmId);
    printf("attach shm success, shmAddr:%p\n", start);

    // 3. 处理
    while (true)
    {
        std::cout << "client say to server:" << start << std::endl;
        struct shmid_ds ds;
        shmctl(shmId, IPC_STAT, &ds);
        printf("size:%d, creator pid:%d, key:0x%x\n\n", ds.shm_segsz, ds.shm_cpid, ds.shm_perm.__key);

        sleep(1);
    }

    // 4. 断开进程与共享内存的连接
    deleteAttach(start);

    sleep(5);

    // 5. 删除共享内存
    deleteShm(shmId);

    return 0;
}